//
//  BVMyVideoAlertView.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/17.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVMyVideoAlertView.h"
#import "UIImageView+WebCache.h"
#import "UIView+CLSetRect.h"

#define CellHeight   300
@implementation BVMyVideoAlertView

-(instancetype)init{
    
    if (self = [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:40/255.0 green:43/255.0 blue:52/255.0 alpha:1.0];
        
        UIView *playBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 100.0, ScreenW, ScreenW*0.58)];
        [self addSubview:playBgView];
        
        CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, playBgView.CLwidth, playBgView.CLheight)];
        
        _playerView = playerView;
        [playBgView addSubview:_playerView];
        _playerView.url = [NSURL URLWithString:@"http://flv.bn.netease.com/videolib3/1605/16/nTaMi2329/HD/nTaMi2329-mobile.mp4"];
        [_playerView playVideo];
        
        [_playerView backButton:^(UIButton *button) {
            [_playerView destroyPlayer];
            _playerView = nil;
            [self removeFromSuperview];
            NSLog(@"返回按钮被点击");
        }];
        
        [_playerView endPlay:^{
            [_playerView destroyPlayer];
            _playerView = nil;
            NSLog(@"播放完成");
        }];
    }
    
    return self;
}

- (UIImage *)getPictureWithName:(NSString *)name
{
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"CLPlayer" ofType:@"bundle"]];
    NSString *path   = [bundle pathForResource:name ofType:@"png"];
    return [UIImage imageWithContentsOfFile:path];
}


- (CGFloat)cellOffset
{
    /*
     - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
     将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
     这里用来获取self在window上的位置
     */
    CGRect toWindow      = [self convertRect:self.bounds toView:self.window];
    //获取父视图的中心
    CGPoint windowCenter = self.superview.center;
    //cell在y轴上的位移  CGRectGetMidY之前讲过,获取中心Y值
    CGFloat cellOffsetY  = CGRectGetMidY(toWindow) - windowCenter.y;
    //位移比例
    CGFloat offsetDig    = 2 * cellOffsetY / self.superview.frame.size.height ;
    //要补偿的位移,self.superview.frame.origin.y是tableView的Y值，这里加上是为了让图片从最上面开始显示
    CGFloat superViewY   = CLscreenHeight - self.superview.frame.size.height;
    CGFloat offset       = -offsetDig * CellHeight / 2 + superViewY;
    
    //让pictureViewY轴方向位移offset
    CGAffineTransform transY = CGAffineTransformMakeTranslation(0,offset);
    _PictureView.transform   = transY;
    
    return offset;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _button.CLcenterX     = self.CLwidth/2.0;
    _button.CLcenterY     = self.CLheight/2.0;
    _PictureView.CLheight = self.CLheight * 2;
}

#pragma mark - 弹出
-(void)showMKPAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.layer.position = self.center;
    self.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_playerView destroyPlayer];
    _playerView = nil;
    [self removeFromSuperview];
}

@end
